//C:8.10:leftover.cpp -- overloading the left() function
#include <iostream>
unsigned long left(unsigned long num, unsigned ct);
char *left(const char *str, int n = 1);

int main(void)
{
    using namespace std;
    char *trip = "Hawaii!!";    // test value
    unsigned long n = 12345678;    // test value
    int i;
    char *temp;

    for(int i = 0; i < 10; i++)
    {
        cout << left(n, i) << '\n';
        temp = left(trip, i);
        cout << temp << '\n';
        delete [] temp;    // point to temporary storage
    }

    return 0;
}

// This function returns the first ct digits of the numer num.
unsigned long left(unsigned long num, unsigned ct)
{
    unsigned digits = 1;
    unsigned long n = num;

    if(ct == 0 || num == 0)
    {
        return 0;    // return 0 if no digits
    }

    while(n /= 10)
    {
        digits++;
    }

    if(digits > ct)
    {
        ct = digits - ct;
        while(ct--)
        {
            num /= 10;
        }
        return num;    // return left ct digits
    }
    else 
    {
        return num;    // return the whole number
    }
}

// This function returns a pointer to a new string
// consisting of the first n character in the str string
char *left(const char *str, int n)
{
    if(n < 0)
    {
        n = 0;
    }
    char *p = new char[n + 1];
    int i;
    for(i = 0; i < n && str[i]; i++)
    {
        p[i] = str[i];    // copy characters
    }

    while(i <= n)
    {
        p[i++] = '\0';    // set rest of string to '\0'
    }
    return p;
}